var app = getApp();
Page({
	data: {
		order: {},
		remark: null,
		opens: 0,
		reasonlist: ["不想要了", "下单重复", "地址有误", "其他"], //原因
		reasonindex: -1,
		orderId: ''
	},
	onLoad(options) {
		var $th = this;
		this.setData({
			orderId: options.id
		})
		app.globalAjax(
			'/api/order/detail',
			"get", {
				id: options.id
			},
			function(e) {
				if (e.code == 0) {
					$th.setData({
						order: e.order
					})
				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			})

	},
	setopens: function() { //弹出
		var $th = this;
		var o = $th.data.opens ? 0 : 1;
		$th.setData({
			opens: o
		});
	},
	selectref: function(e) { //选择
		var $th = this,
			cur = e.currentTarget.dataset;
		$th.setData({
			reasonindex: cur.index
		});
		$th.setopens()
	},
	sub: function() {
		var that = this;
		if (that.data.reasonindex == -1) {
			wx.showToast({
				title: "请选择退款原因",
				icon: "none"
			});
			return false;
		}

		var post = {
			reason: that.data.reasonlist[that.data.reasonindex], //原因
			remark: that.data.remark,
			totalAmount: that.data.order.totalAmount,
			orderId: that.data.order.orderId
		}
		app.globalAjax(
			'/api/order/refund',
			"post",
			post,
			function(e) {
				if (e.code == 0) {

					var pages = getCurrentPages(); //获取页面栈
					if (pages.length > 1) {
						//上一个页面实例对象
						var prePage = pages[pages.length - 2];
						//调用上一个页面的onShow方法
						prePage.onLoad()
					}
					wx.navigateBack({
						delta: 1
					})
				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			})
	},
	inp: function(e) { //监听输入
		this.setData({
			remark: e.detail.value
		})
	}
})
